/**
 * 2017年2月24日
 * 温州两张
 */

var cardPool = require('./cardPool');

var log = console.log;

// 点数小的排前面
function sortByNum(a, b) {
    return parseInt(a.substring(1)) - parseInt(b.substring(1));
}

// 测试是否是一副红牌
function suitTest(arr, type) {
    if (type && type === 'b') {
        // 如果要测试黑牌
        return arr.join('') === 'DB' || arr.join('') === 'BD';
    }
    return arr.join('') === 'AC' || arr.join('') === 'CA';
}

// 测试点数
function numTest(arr, str) {
    return arr.join('') === str;
}

// 左侧补足
// 默认为0
function leftPad(strs, len, str) {
    if (!str) {
        str = '0';
    }
    if (!len || len <= 0 || strs.length >= len) {
        return strs;
    }

    for (var i = strs.length; i < len; i++) {
        strs = str + strs;
    }
    return strs;
}

// 获取两张牌的牌型
function getCardType(arr) {

    // log(arr);
    let card = [];

    for (let v of arr) {
        card.push(v);
    }

    var rel = {
        card,
        type: 0,
        realPoint: 9,
        max: '',
        weight: ''
    };

    arr.sort(sortByNum);

    var cardNum = [],
        cardSuit = [];

    for (let v of arr) {
        cardNum.push(parseInt(v.substring(1)));
        cardSuit.push(v.substring(0, 1));
    }

    // log(cardNum, cardSuit);

    if (numTest(cardNum, '1212')) {
        // 1: 对天，两个Q
        rel.type = 1;
        rel.max = 12;
    } else if (numTest(cardNum, '22')) {
        // 2: 对地，两个2
        rel.type = 2;
        rel.max = 2;
    } else if (numTest(cardNum, '13')) {
        // 3: 皇帝，1和3
        rel.type = 3;
        rel.max = 3;
    } else if (numTest(cardNum, '88') && suitTest(cardSuit)) {
        // 4: 对人，两个红8
        rel.type = 4;
        rel.max = 8;
    } else if (numTest(cardNum, '44') && suitTest(cardSuit)) {
        // 5: 对合，两个红4
        rel.type = 5;
        rel.max = 4;
    } else if ((numTest(cardNum, '1010') || numTest(cardNum, '66') || numTest(cardNum, '44')) && suitTest(cardSuit, 'b')) {
        // 6: 两个黑10/6/4
        rel.type = 6;
        rel.max = cardNum[0];
    } else if (((numTest(cardNum, '1010') || numTest(cardNum, '77') || numTest(cardNum, '66')) && suitTest(cardSuit)) || (numTest(cardNum, '1111') && suitTest(cardSuit, 'b'))) {
        // 7: 两个红10/6/7 或者两个黑J
        rel.type = 7;
        rel.max = cardNum[0];
    } else if (((numTest(cardNum, '88') || numTest(cardNum, '77')) && suitTest(cardSuit, 'b')) || ((numTest(cardNum, '99') || numTest(cardNum, '55')) && suitTest(cardSuit))) {
        // 8: 两个红9/5 或者两个黑7/8
        rel.type = 8;
        rel.max = cardNum[0];
    } else if (numTest(cardNum, '912')) {
        // 9: 一个Q一个9
        rel.type = 9;
        rel.max = 12;
    } else if (numTest(cardNum, '812')) {
        // 10: 一个Q一个8
        rel.type = 10;
        rel.max = 12;
    } else if (numTest(cardNum, '28')) {
        // 11: 一个8一个2
        rel.type = 11;
        rel.max = 8;
    } else {
        // 12: 只能比大小了
        rel.type = 12;
        rel.max = cardNum[1];
    }

    if (rel.type === 12) {
        if (cardNum[0] === 1) {
            rel.realPoint = (cardNum[0] + cardNum[1] + 5) % 10;
        } else {
            rel.realPoint = (cardNum[0] + cardNum[1]) % 10;
        }
    }

    rel.weight = parseInt(leftPad((16 - rel.type).toString(2), 4) + leftPad(rel.realPoint.toString(2), 4) + leftPad(rel.max.toString(2), 4), 2);

    // log(rel);

    return rel;

}

function next(rid) {

    var player = [];
    var curCards, rel = {};


    for (let i = 0; i < 4; i++) {
        player[i] = {};
        player[i].card = [];

        player[i].card.push(cardPool.next());
        player[i].card.push(cardPool.next());

        if (reCards && reCards[i * 2] && reCards[(i * 2) + 1]) {
            player[i].card[0] = reCards[i * 2];
            player[i].card[1] = reCards[(i * 2) + 1];
        }

        if (i === 0) {
            curCards = player[i].card.join();
        }
        else {
            curCards += ('_' + player[i].card.join());
        }

        player[i] = getCardType(player[i].card);

    }

    // console.dir(player, { showHidden: false, depth: null, colors: true });
    rel.curCards = curCards;
    rel.player = player;

    return rel;
}

var reCards = [];

// 如果顶替，就一直顶替
function replaceCards(arr) {
    reCards = arr;
    return true;
}

var exp = module.exports;

exp.next = next;
exp.replaceCards = replaceCards;
